package com.cjm.connect;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class test8 {
    private static String user;
    private static String password;
    private static String url;
    private static String driver;
    static{
        try {
//            创建连接对象
            Properties properties = new Properties();
            properties.load(new FileInputStream("src/connect"));
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            url = properties.getProperty("url");
            driver = properties.getProperty("driver");
        } catch (IOException e) {
            //编译异常转换成运行异常
            throw new RuntimeException(e);
        }
    }
    public static Connection getConnection(){

        try {
            return DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

//    关闭流
    public static void close(Connection con, ResultSet rs, Statement st) {

        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (con != null) {
                con.close();
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
